import java.util.ArrayList;
import java.awt.Color;
/**
 * Board the egame is played on. It is a supclass of caddy and defines 
 * 
 * @author Amino
 */
public class Board extends _Caddy
{

    mainPanel1 MP;
    BackGround1 sampleBG;
    Card c;
    Hand h;
    Answers a;
    ArrayList<Button> myButtons = new ArrayList<Button>();
    ArrayList<Button> selectedButtons = new ArrayList<Button>();
    CAHClient client;
    boolean Selecting = false;
    boolean Voting = false;
    boolean deleteAnswers = false;
    boolean beenJury = false;
    Button akt;
    boolean done = false;
    long startCD = 0;
    long CD = 3000;
    /**
     * Constructor for objects of class Board
     */
    public Board(int b, int h, mainPanel1 mP, CAHClient client)
    {
        this.breite = b;
        this.hohe = h;
        this.MP = mP;
        this.client = client;
        this.sampleBG = new BackGround1( this);

        addObject(this.sampleBG);
        this.h =  new Hand(this);
        this.a = new Answers(this);
        drawings.add(this.h);
        drawings.add(this.a);
        
         akt = new Button(new SAAnswer(this)
        , new Vec2(250,455), new Vec2(200, 50),new Color(70,220,70), "");
        myButtons.add(akt);
        addObject(akt);
        
        PunkteSystem ps = client.p;
        PunkteSystemer psys = new PunkteSystemer(ps);
        drawings.add(psys);
        done = true;
    }
    public void SetBlackCard(String s){
        RemoveBlackCard();
        SelectionAction SA = new SelectionAction(this);
        c = new Card(250,25,this, SA,true,s);
        SA.setButton(c);
        myButtons.add(c);
        addObject(c);
    }
    public void AddHandCard(String s){
        h.insert(this, s);
        
    }
    public void cleanHand(){
        int size = h.myHand.size();
        for(int i = 0; size > i ; i ++){
            deleteButton(h.myHand.get(0));
            h.myHand.remove(0);
        }
        h.selected.clear();
    }
    public void deleteButton(Button b){
        if(b == null)return;
        for(int i = 0; i< myButtons.size(); i++)
        {
            if(myButtons.get(i).getText().equals(b.getText()))myButtons.remove(i);
        }
    }
    public void cleanAnswers(){
        int size = a.myHand.size();
        deleteAnswers = false;
        for(int i = 0; size > i ; i ++){
            deleteButton(a.myHand.get(0));
            a.myHand.remove(0);
        }
    }
    public void RemoveBlackCard(){
        deleteObejct(c);
        deleteButton(c);
    }
    double getVX() {
        return .0;
    }
    public void update(){
        if(!done)return;
        if(client==null)
        return;
        updateWinCD();
        if(!deleteAnswers && !CD())
            updateKarten();
        if(!deleteAnswers && !CD())
            updateAntwort();
        if(!deleteAnswers && !CD())
            updateFrage();
        
    }
    private void updateWinCD(){
        if(!CD() && deleteAnswers){
            cleanAnswers();
        }
        else if(client.updateSieg){
            client.updateSieg = false;
            if(!beenJury)
            {
                a.getCardWith(client.gewinner.get(client.gewinner.size()-1)).
                drawGreen = true;
                startCD = System.currentTimeMillis();
                deleteAnswers = true;
            }
            else
            beenJury = false;
        }
    }
    private void updateKarten(){
        if(client.updateKarten){
            client.updateKarten = false;
            cleanHand();
            for(int i = 0; client.karten.size() > i ; i ++){
                h.insert(this, client.karten.get(i));
            }
            if(!client.binJury){
                enableAnswer();
            }
            else{
                beenJury = true;
                akt.setText("Warte auf Karten");
            }
        }
    }
    private void updateAntwort(){
        if(client.updateAntwort){
            client.updateAntwort = false;
            cleanAnswers();
            for(int i = 0; client.antworten.size() > i ; i ++){
                a.insert(this, client.antworten.get(i));
            }
            if(client.binJury){
                enableVoting();
            }
            else{
                akt.setText("Warte auf Urteil");
            }
        }
    }
    private void updateFrage(){
        if(client.updateFrage){
            client.updateFrage = false;
            SetBlackCard(client.dieFrage);
            
        }
        
    }
    private void enableAnswer(){
        h.enbaleSelection(client.frageInt);
        Selecting = true; 
        SAAnswer a = new SAAnswer(this);
        akt.setAction(a);
        akt.setText("Wähle Karten");
    }
    private void enableVoting(){
        Voting = true;
        SAVoting a = new SAVoting(this);
        akt.setAction(a);
        akt.setText("Urteile Antworten");
    }
    double getVY() {
        return .0;
    }
    public void mouseEvent(int x, int y, boolean click){
        for(int i = 0; i < myButtons.size(); i++){
            myButtons.get(i).mouseOn(new Vec2(x,y), click);
        }
    }
    public void select(Button but){
        if(but instanceof Card && h.canSelect((Card)but)){
            if(Selecting)
                but.switchSelect();
        }
        else if(but instanceof Card  && a.canSelect((Card)but)){
            if(Voting)
                but.switchSelect();
        }
        
    }
    public void GiveAnswer(){
        if(Selecting && h.selected.size() == client.frageInt){
            if(client.frageInt == 1)
            {
                 h.selected.get(0).drawGreen = true;
                 int i = h.getNummer(h.selected.get(0));
                 client.karteWaehlen(i+1);
            }
            if(client.frageInt == 2)
            {
                 h.selected.get(0).drawGreen = true;
                 h.selected.get(1).drawGreen = true;
                client.karteWaehlen(h.getNummer(h.selected.get(0))+1
                    ,h.getNummer(h.selected.get(1))+1);
            }
            Selecting = false;
        }
    }
    public void GiveVoting(){
        if(Voting && a.getSelected()!=null){
            client.wahlGeben(a.getSelected().getText());
            Voting = false;
            startCD = System.currentTimeMillis();
            a.getSelected().drawGreen = true;
            deleteAnswers = true;
        }
    }
    public boolean CD(){
        return startCD + CD > System.currentTimeMillis();
    }
}
